<?php 
session_start();
include_once 'inc.php' ;

if( $_GET['action'] == "login" ){
	$pwd = md5($config['password']);
	if( md5(trim($_POST['pwd'])) == $pwd ){
		$_SESSION['isLogin'] = $config['hash'];
		echo '{"info":"1"}';
	}else{
		echo '{"info":"0"}';
	}
	//header("Location: ".$config['admin']);
}elseif( $_GET['action'] == "logout" ){
	$_SESSION['isLogin'] = "";
}elseif( $_GET['action'] == "content" ){
	if( $_SESSION['isLogin'] == $config['hash'] ){
		echo getContent();
	}else{
		header("Location: ".$config['admin']);
	}
}elseif( $_GET['action'] == "info" ){
	if( $_SESSION['isLogin'] == $config['hash'] ){
		echo getInfo();
	}else{
		header("Location: ".$config['admin']);
	}
}elseif( $_GET['action'] == "add" ){
	if( $_SESSION['isLogin'] == $config['hash'] ){
		if( trim($_POST['content']) != "" ){
			add($_POST['content']);
		}else{
			header("Location: ".$config['admin']);
		}	
	}else{
		header("Location: ".$config['admin']);
	}
}elseif( $_GET['action'] == "clear" ){
	if( $_SESSION['isLogin'] == $config['hash'] ){
		clear();
	}else{
		header("Location: ".$config['admin']);
	}
}else{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $config['site_name']; ?> - Powered by mrasong.com</title>
<meta name="robots" content="noindex,nofollow" />
<link rel="stylesheet" href="style.css" />
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<div class="header">
	<div class="nav"><?php echo $config['site_name']; ?>-后台管理</div>
</div>
<div class="content">
	<?php if( $_SESSION['isLogin'] ) { ?>

	<div class="add_form">
    	<form id="addto" onsubmit="return false" action="?action=add" method="post">
    	<div><textarea id="content" name="content"></textarea></div>
        <div align="right"><i>在上面加入要添加的卡号，每卡一行，不要留空行。</i>
        <input class="btn" id="submit" type="submit" value="添加" />
        <input class="btn" id="clear" type="button" value="清空数据" />
        <input class="btn" id="logout"  type="button" value="安全退出" /></div>
        </form>
    </div>
    <h3>卡信息：<span id="sno">加载中...</span> <input type="button" class="btn" id="reload" value="查看详情" />
     <input type="button" class="btn" id="close" value="关闭" /></h3>
    <div class="data"></div>
    <div id="alert"></div>
	<script type="text/javascript">
    $(document).ready(function(){
		$.get("?action=info", function(d){
			$("#sno").html("还剩 "+ d + " 张");
		}); 
		$("#logout").click(function(){
			$.get("?action=logout");
			$("#alert").html("您已经成功退出！");
			$("#alert").slideDown(500);
			setTimeout(function(){window.location.href = "<?php echo $config['admin'] ;?>";},2000);
		});	
		$("#clear").click(function(){
			$.get("?action=clear", function(d){
				if( d == "0" ){
				    $tmp_str = "清除失败@@@";
				}else{
					$tmp_str = "数据已经全部清除！";
				}				
			});	
			
			$("#alert").html($tmp_str);
			$("#alert").slideDown(500);
			setTimeout(function(){window.location.href = "<?php echo $config['admin'] ;?>";},2000);
		});	
		$("#close").click(function(){
			$(".data").slideUp(500);
			$(this).hide();
			$("#reload").val("查看详情");
		});	
    	$("#reload").click(function(){
			$(this).val("重新加载");
			$("#close").show();
			$(".data").html("<div style='color:#AA0000;font-size:16px;margin:20px;'>加载中，请稍候...</div>");
			today = new Date();
			now = today.getTime();
			$.get("?action=content", function(d){
				if( d == "" ){
					$(".data").html("<div style='color:#AA0000;font-size:16px;margin:20px;'>没有数据，请添加</div>");
				}else{
					$(".data").html('<textarea readonly="readonly">'+ d +'</textarea>');
				}
				$(".data").slideDown(500);
			});	
		});
		$("#addto").submit(function(){
			$("#alert").html("数据提交中...");
			$("#alert").slideDown(500);
			if( $("#content").val() == "" ){
				$("#alert").html("内容不能为空！！！");
				$("#alert").slideDown(500);
				setTimeout(function(){ $("#alert").slideUp(500) },2000);
			}else{
				$.ajax({
					url: "?action=add",
					type: "POST",
					data: "content=" + $("#content").val(),
					dataType: "json",
					success: function(d){
						if( d.info == '0' ){
							$("#alert").html("添加失败，请重试！").slideDown(500);
							setTimeout(function(){ $("#alert").slideUp(500) },2000);
						}else{
							$("#sno").html("还剩 "+ d.no + " 张");
							$("#alert").html("添加成功<br />共 " + d.no + " 条数据！");
							$("#alert").slideDown(500);
							$("#content").val("");
							setTimeout(function(){ $("#alert").slideUp(500) },2000);
						}
					}			
				});	
			}
			return false;
		});
	})
    </script>

	<?php }else{ ?>

	<div class="login_form">
    	<form id="login" onsubmit="return false" action="?action=login" method="post">
    	<h3><label for="pwd"><?php echo $config['site_name']; ?></label></h3>
        <p><input class="text" type="password" name="pwd" id="pwd" /><input class="btn" type="submit" value="登陆" /></p>
        <p><span id="tips">请输入密码登陆</span></p>
        </form>
    </div>
	<script type="text/javascript">
    $(document).ready(function(){
    	$("#login").submit(function(){
			if( $("#pwd").val() == "" ){
				$("#tips").hide();
				$("#tips").html("密码不能为空！").slideDown(500);
			}else{
				$.ajax({
					url: "?action=login",
					type: "POST",
					data: "pwd=" + $("#pwd").val(),
					dataType: "json",
					success: function(d){
						if( d.info == '0' ){
							$("#tips").hide();
							$("#tips").html("密码错误，请重新输入！").slideDown(500);
						}else{
							$("#tips").html("登录成功，载入中...");
							window.location.href = "<?php echo $config['admin'] ;?>";
						}
					}			
				});		
			}
			return false;
		});
	})
    </script>

	<?php } //end if is login ?>
</div>
<div class="copy">
<a href="https://me.alipay.com/mrasong">捐赠</a>
<a href="http://mrasong.com" target="_blank">&copy;Mr.Asong</a></div>
</body>


</html>
<?php } //end if  post ?>